//自己实现一个双向链表
public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;  //标记双向链表的头部
    public ListNode tail;  //标记双向链表的尾部

    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        int cnt = 0;
        ListNode cur = this.head;
        while (cur != null) {
            cnt++;
            cur = cur.next;
        }
        return cnt;
    }

    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
            this.tail = node;
        } else {
            node.next = this.head;
            this.head.prev = node;
            head = node;
        }
    }

    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
            this.tail = node;
        } else {
            this.tail.next = node;
            node.prev = this.tail;
            this.tail = node;
        }
    }

    public void addIndex(int index, int data) {
        //1.判断index合法性
        if (index < 0 || index > size()) {
            throw new IndexWrongFulException("index位置不合法");
        }
        //2.判断特殊位置 首尾
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        //3.找到index位置
        ListNode cur = findIndexListNode(index);
        //4.修改4个指向
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    private ListNode findIndexListNode(int index) {
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {   //cur是头结点的情况
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        tail = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {   //不是尾结点
                        cur.next.prev = cur.prev;
                    } else {
                        this.tail = cur.prev;   //如果是尾结点，直接让tail指向
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    public void removeAllkey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {   //cur是头结点的情况
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        tail = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {   //不是尾结点
                        cur.next.prev = cur.prev;
                    } else {
                        this.tail = cur.prev;   //如果是尾结点，直接让tail指向
                    }
                }
            }
            cur = cur.next;
        }
    }

    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }
}

